"""
## 发布动态


ImageOrVideo = (
    (0, "照片"),
    (1, "视频")
)


@csrf_exempt
@api_auth_required
@validate_params("POST", [
    {"name": "content", "clean_func": json.loads, "required": True},
    {"name": "image", "clean_func": json.loads, "required": False},
    {"name": "video", "clean_func": json.loads, "required": False},
    {"name": "position", "clean_func": json.loads, "required": False},
    {"name": "longitude", "clean_func": json.loads, "required": False},
    {"name": "latitude", "clean_func": json.loads, "required": False},
])
def issue_new(request, cleaned_params):
    """
    # 发布动态
    """
    new = News(content=cleaned_params["content"])
    for field in ("image", "video", "position", "longitude", "latitude"):
        if cleaned_params[field]:
            setattr(new, field, cleaned_params[field])
    new.user = request.user
    new.save()
    return api_success_response({})
    

"""

[{"url": "https://hango-public.oss-cn-beijing.aliyuncs.com/F7CA6CE3-42AC-4454-BB83-07640A628A47.jpg", "width": 828, "height": 549}]

[{"url": "http://hango-public.oss-cn-beijing.aliyuncs.com/689e7132-4d1e-4985-ace4-daa3ff407b43.mp4", "height": 1280, "width": 720}]

"""
@csrf_exempt
@api_auth_required
@validate_params("GET", [
    {"name": "new_id", "clean_func": int, "required": True},
])
def new_detail(request, cleaned_params):
    new = News.objects.get(id=cleaned_params["new_id"])
    result = {}
    for field in ("id", "content", "position", "longitude", "latitude"):
        result[field] = getattr(new, field) if getattr(new, field) else ""
    if new.image:
        result["image"] = json.loads(new.image)
    if new.video:
        result["video"] = json.loads(new.video)
    else:
        result["video"] = []
    result["created"] = new.created.strftime("%Y-%m-%d %H:%M:%S") if new.created else None
    result["up"] = new.up.all().count()
    result["user"] = get_user_info(new.user)
    result["is_up"] = False
    result["user"]["is_attention"] = False
    if UserRelation.objects.filter(user=request.user, object_user=new.user, relation=UserRelationStatus.ATTENTION).count() > 0:
        result["user"]["is_attention"] = True
    if new.up.filter(id=request.user.id).count() > 0:
        result["is_up"] = True
            
    result["comment"] = new.comment_new.count()

    comments = []
    comments_obj = new.comment_new.filter(new=new).order_by('-created')
    for comment in comments_obj:
        if request.user:
            user_relation = UserRelation.objects.filter(
                user=request.user, object_user=new.user, relation=UserRelationStatus.BLACK)
            if user_relation.count() > 0:
                continue
        obj_dict = {}
        obj_dict["id"] = comment.id
        obj_dict["content"] = comment.content
        obj_dict["created"] = comment.created.strftime("%Y-%m-%d %H:%M:%S") if comment.created else None
        obj_dict["user"] = get_user_info(comment.user)
        comments.append(obj_dict)
    return api_success_response({"new": result, "comments": comments})
"""